import { Component, OnInit } from '@angular/core';
import {LoginService} from '../../lib/service/login.service';
import {Router} from '@angular/router';
import {HomeConfigService} from "../../lib/service/home/home-config.service";
import {adSystemApiUrl} from '../../../config/api';
import {LocalStorageService} from '../../lib/service/local-storage.service';

export class LoginData {
  code: number;
  data: {
    user: {
      id: number,
      user_uuid: string,
      name: string,
      nick_name: string,
      email: string,
      mobile: string,
      remember_token: string,
      sex: string,
      avatar: string,
      isLock: number,
      lock_time: Date,
      openning_time: Date,
      created_at: Date,
      updated_at: Date,
      remark: string,
      time: string,
      a: any,
      p: string
    },
    token:{
      "access_token": string,
      "token_type": string,
      "expires_in": number,
      "expires_at": Date
    }
  };
  message: string;
  time: string;
}

export class RedirectMsg {
  mobile: number;
  code: number;
  password: string;
  password_rep: string;
}

@Component({
  selector: 'app-register',
  templateUrl: './register.component.html',
  styleUrls: ['./register.component.scss'],
  providers: [
    LoginService,
    HomeConfigService,
    LocalStorageService
  ]
})
export class RegisterComponent implements OnInit {

  vUrl = '';
  table_id;
  verificate;
  redirectSmsSendTime = 0;
  loginData = new LoginData();
  redirectMsg = new RedirectMsg();

  constructor(private loginService: LoginService,
              private router: Router,
              private homeConfigService: HomeConfigService,
              private localStorageService: LocalStorageService) {
  }

  ngOnInit() {
    this.vCode();
  }

  vCode() {
    this.vUrl = '';
    this.loginService.getTableId().subscribe(
      rs => {
        this.table_id = rs.data.table_id;
        this.vUrl = adSystemApiUrl + '/v1/common/captcha/get_captcha/' + rs.data.table_id;
      },
      err => console.log(err),
    );
  }

  getPhoneCode(type: number, mobile: number) {
    if (!mobile) {
      return alert('请填写手机号码');
    }
    this.loginService.checkCode(this.verificate, this.table_id).subscribe(
      data => {
        if(data.code === 2000) {
          this.smsSend(type, mobile);
        }
        else {
          alert(data.msg);
          this.vCode();
        }
      }
    );
  }

  smsSend(type: number, mobile) {
    this.loginService.smsSend(mobile, type).subscribe(
      res => {
        if (res.code === 2000) {
          this.redirectSmsSendTime = 60;
          this.redirectSmsSendTimeLoading();
          alert(res.message);
        } else {
          alert(res.message);
        }
      }
    );
  }

  // 验证码倒计时
  redirectSmsSendTimeLoading() {
    const Loading = setInterval(() => {
      this.redirectSmsSendTime = this.redirectSmsSendTime - 1;
      if (!this.redirectSmsSendTime) {
        clearInterval(Loading);
      }
    }, 1000);
  }

  // 注册
  register() {
    this.loginService.register(this.redirectMsg).subscribe(
      res => {
        if (res.code === 2000) {
          this.loginService.doLogin(this.redirectMsg.mobile, this.redirectMsg.password).subscribe(
            rs => {
              if (rs.code === 2000) {
                this.loginData = rs;
                const time = new Date(rs.time);
                const now_time = time.getTime() / 1000;
                this.loginData.data.user.time = now_time + '';
                this.loginData.data.user.a = this.redirectMsg.mobile;
                this.loginData.data.user.p = this.redirectMsg.password;
                // 清理之前登录的信息
                this.localStorageService.clearAll();
                // 保存当前的信息
                this.localStorageService.setUserInfo(this.loginData.data.user);
                this.localStorageService.setToken(this.loginData.data.token);
                return this.redirect();
              } else {
                alert(rs.message);
              }
            },
            err => console.log(err),
          );
        } else {
          alert(res.message);
        }
      }
    );
  }

  redirect() {
    // if (this.oauthService.isLogin()) {
    this.router.navigate(['/main/index']);
    // } else {
    //   console.log('未登录');
    // }
  }

}
